package pl.factat.gfk;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: adam
 * Date: 14.12.12
 * Time: 16:27
 * To change this template use File | Settings | File Templates.
 */
public class ImagePanel extends JPanel {
    private BufferedImage image = null;

    private final boolean drawCircle = true;
    private final int circleSize = 10;
    private final int filledSize = 3;

    public ImagePanel() {
        setBackground(Color.black);
    }

    public void setImage(String f) {

        try {
            image = ImageIO.read(new File(f));
            //System.out.println("setting image");
            setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
            repaint();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setImage(BufferedImage img) {
        if(img == null) return;
        this.image = img;
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
        repaint();
    }

    public BufferedImage getImage() {
        return image;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        if (image != null) {
            try {
                g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawImage(image, 0, 0, null);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Could not load image ");
            }
        if(drawCircle)
        {
            drawSkewFrame(g2d);
        }
        }
    }

    public int[] getImageData() {
        return ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    }

    private void drawSkewFrame(Graphics2D g2d)
    {
        g2d.setColor(new Color(99, 131, 255));
        g2d.drawOval(0, 0, circleSize, circleSize);

        g2d.drawOval(image.getWidth()-circleSize, 0, circleSize, circleSize);

        g2d.drawOval(image.getWidth()-circleSize, image.getHeight()-circleSize, circleSize, circleSize);

        g2d.drawOval(0, image.getHeight()-circleSize, circleSize, circleSize);

        g2d.drawLine(circleSize/2, circleSize/2, image.getWidth()-circleSize/2, circleSize/2);
        g2d.drawLine(circleSize/2, circleSize/2, circleSize/2, image.getHeight()-circleSize/2);
        g2d.drawLine(image.getWidth()-circleSize/2, circleSize/2, image.getWidth()-circleSize/2, image.getHeight()-circleSize/2);
        g2d.drawLine(circleSize/2, image.getHeight()-circleSize/2, image.getWidth()-circleSize/2, image.getHeight()-circleSize/2);

    }
}
